package dev.alm.java11boot276.controller;

import dev.alm.java11boot276.common.Result;
import dev.alm.java11boot276.entity.User;
import dev.alm.java11boot276.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("add")
    public Result add(@RequestBody User user) {
        User add = userService.add(user);
        if (add == null) {
            return Result.fail("添加用户失败，请重试！");
        }
        return Result.success(add);
    }

    @PostMapping("login")
    public Result login(@RequestBody User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        if (!username.equals("admin") || !password.equals("abc123")) {
            return Result.fail("用户名或密码错误");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", "admin");
        return Result.success(map);
    }

    @PostMapping("logout")
    public Result logout() {
        Result r = Result.success(null);
        r.setMessage("退出成功");
        return r;
    }

    @PostMapping("list")
    public Result list() {
        return Result.success(userService.list());
    }

}
